(function (d, n) {
    var $ = function (options) {
        var defaultOptions = {
            id: "MSAF"
        };

        this.options = Object.assign({}, defaultOptions, options);
        this.checkedOptions();
        this.initParams();
        this.initDOMs();

        this.transformJSON = {
            flip: 0,
            rotate: 0,
            scale: 0
        };

        this.setOrigin();
        this.init();
    };


    $.prototype = {
        init: function () {
            var that = this;
            this.resizeBtn.addEventListener("touchmove", function (e) {
                e.stopPropagation();

                var touches = e.touches[0],
                    currentX = touches.pageX,
                    currentY = touches.pageY,
                    distance = Math.sqrt((currentX - that.OX) * (currentX - that.OX) + (currentY - that.OY) * (currentY - that.OY)),
                    a = Math.sqrt((currentX - that.flipX) * (currentX - that.flipX) + (currentY - that.flipY) * (currentY - that.flipY));


                //计算当前旋转角度
                var angle = Math.acos((that.OR * that.OR + distance * distance - a * a) / (2 * that.OR * distance)) * 180 / Math.PI;
                //判断点在直线哪侧
                var cacheY = (that.OY - that.flipY) / (that.OX - that.flipX) * currentX + (that.flipY * that.OX - that.OY * that.flipX) / (that.OX - that.flipX);
                if (currentY <= cacheY) {
                    angle = 180 + 180 - angle
                }

                //计算缩放比例
                var scale = distance / that.OR,
                    unScale = that.OR / distance;


                //进行变换
                that.transformJSON.scale = scale;
                that.transformJSON.rotate = angle;


                that.dom.style.transform = "scale(" + that.transformJSON.scale + ") rotateZ(" + that.transformJSON.rotate + "deg)";
                that.dom.style.webkitTransform = "scale(" + that.transformJSON.scale + ") rotateZ(" + that.transformJSON.rotate + "deg)";

                that.deleteButton.style.transform = "scale(" + unScale + ")";
                that.deleteButton.style.webkitTransform = "scale(" + unScale + ")";

                that.flipButton.style.transform = "scale(" + unScale + ")";
                that.flipButton.style.webkitTransform = "scale(" + unScale + ")";

                that.resizeBtn.style.transform = "scale(" + unScale + ")";
                that.resizeBtn.style.webkitTransform = "scale(" + unScale + ")";
            });


            this.resizeBtn.addEventListener("touchend", function (e) {
                that.options.resizeItemCallback && that.options.resizeItemCallback.call(that);
            });

            this.flipButton.onclick = function (e) {
                e.stopPropagation();
                that.transformJSON.flip = that.transformJSON.flip === 180 ? 0 : 180;

                that.dom.querySelector('div').style.transform = "rotateY(" + that.transformJSON.flip + "deg)";
                that.dom.querySelector('div').style.webkitTransform = "rotateY(" + that.transformJSON.flip + "deg)";

                that.options.flipItemCallback && that.options.flipItemCallback.call(that);
            };


            this.deleteButton.onclick = function (e) {
                e.stopPropagation();
                that.dom.parentNode.removeChild(that.dom);
                that.options.deleteItemCallback && that.options.deleteItemCallback.call(that);
            }
        },
        setOrigin: function () {
            var width = this.dom.offsetWidth,
                height = this.dom.offsetHeight,
                left = this.dom.offsetLeft,
                top = this.dom.offsetTop;

            this.offsetX = left;
            this.offsetY = top;
            this.OR = Math.sqrt(width / 2 * width / 2 + height / 2 * height / 2);
            this.OX = this.offsetX + width / 2;
            this.OY = this.offsetY + height / 2;
            this.flipX = left + width;
            this.flipY = top + height;
        },
        initDOMs: function () {
            var buttonSize = 20,
                position = -1 * buttonSize / 2;

            this.dom.style.position = 'relative';
            this.dom.querySelector('div').style.height = '100%';
            this.dom.querySelector('div').style.width = '100%';

            var deleteButton = d.createElement('i'),
                flipButton = d.createElement('i'),
                resizeButton = d.createElement('i');


            setButtonStyle(deleteButton, './image/delete.png', 'auto', 'auto', position, position);
            setButtonStyle(flipButton, './image/flip.png', position, 'auto', 'auto', position);
            setButtonStyle(resizeButton, './image/resize.png', 'auto', position, position, 'auto');

            function setButtonStyle(dom, path, top, right, bottom, left) {
                dom.style.display = 'block';
                dom.style.position = 'absolute';
                dom.style.left = left + 'px';
                dom.style.top = top + 'px';
                dom.style.bottom = bottom + 'px';
                dom.style.right = right + 'px';
                dom.style.width = buttonSize + 'px';
                dom.style.height = buttonSize + 'px';
                dom.style.backgroundSize = '100%';
                dom.style.backgroundImage = 'url(' + path + ')';
            }

            this.dom.appendChild(deleteButton);
            this.dom.appendChild(flipButton);
            this.dom.appendChild(resizeButton);

            this.deleteButton = deleteButton;
            this.flipButton = flipButton;
            this.resizeBtn = resizeButton;
        },
        initParams: function () {
            try {
                this.dom = document.getElementById(this.options.id);
            } catch (e) {
                console.error('id为' + this.options.id + '的DOM不存在');
            }
        },
        checkedOptions: function () {
            if (!this.options.id) {
                console.error('参数id必传');
                return;
            }
        }
    };

    window[n] = function (options) {
        return new $(options);
    };

})(document, 'MSAF');